import path from 'path'
// import bodyParser from 'koa-bodyparser'
import Pug from 'koa-pug'
import favicon from 'koa-favicon'
import staticCache from 'koa-static-cache'
import error from 'koa-error'
import routes from '../routes/'
import koaBody from 'koa-body'
import mock from './mock'
const isDev = process.env.NODE_ENV === 'development'
const pug = new Pug({
  viewPath: './views',
  debug: isDev,
  pretty: !isDev,
  cache: true,
  basedir: '../views',
  helperPath: [{
    _: require('lodash')
  }]
})
export default (app) => {
  app.use(staticCache(path.join(__dirname, '../dist'), {
    maxAge: 365 * 24 * 60 * 60
  }))
  app.use(staticCache(path.join(__dirname, '../static'), {
    maxAge: 365 * 24 * 60 * 60
  }))
  app.use(favicon(path.join(__dirname, '../static/images/favicon-96x96.png')))
  app.use(koaBody({
    multipart: true
  }))
  pug.use(app)
  app.use((ctx, next) => {
    ctx.state.ENV = process.env.NODE_ENV
    return next()
  })
  app.use(mock)
  app.use(routes.routes())
  app.use(routes.allowedMethods())
  app.use(error({
    engine: 'pug',
    template: path.resolve(__dirname, '../views/error.pug')
  }))
}
